
###########################################
# USER CONFIG ++++++
#1.multi src dir
#2.multi suffix
###########################################
USER_CC      := g++
USER_INC_DIR := include
USER_SRC_DIR := src/OpenCV
USER_OUT_DIR := /tmp/out
USER_SUFFIX  := .cpp
USER_EXCEPTS := OpenCVNevenTest.cpp OpenCVTestPC.cpp OpenCVNOTinGITSaveFace.cpp 
USER_CFLAGS  := -Wall -g -fPIC
USER_LIBS    := `pkg-config --libs opencv` -lneven
USER_MODULE  := run
###########################################
# USER CONFIG -------
###########################################

LOCAL_CC := $(USER_CC) 
#LOCAL_PATH := $(dir $(lastword $(MAKEFILE_LIST)))
LOCAL_C_INCLUDES := $(strip $(USER_INC_DIR))
LOCAL_SRC_DIR := $(strip $(USER_SRC_DIR))
LOCAL_OUT_DIR := $(strip $(USER_OUT_DIR))
LOCAL_SUFFIX := $(strip $(USER_SUFFIX))
LOCAL_EXCEPTS := $(foreach name, $(strip $(USER_EXCEPTS)), $(LOCAL_SRC_DIR)/$(name))
LOCAL_CFLAGS := $(foreach onedir, $(LOCAL_C_INCLUDES), -I$(onedir)) $(USER_CFLAGS)
LOCAL_LDLIBS := $(USER_LIBS)
LOCAL_MODULE := $(LOCAL_OUT_DIR)/$(USER_MODULE)

LOCAL_SRC_FILES := $(filter-out $(LOCAL_EXCEPTS), $(wildcard  $(LOCAL_SRC_DIR)/*$(LOCAL_SUFFIX)))
LOCAL_OBJECTS := $(foreach name, $(LOCAL_SRC_FILES), $(LOCAL_OUT_DIR)/$(basename $(notdir $(name))).o)
LOCAL_DEPENDS := $(LOCAL_OBJECTS:.o=.d)

RUN_ONCE := $(shell mkdir -p $(LOCAL_OUT_DIR))

$(LOCAL_MODULE): $(LOCAL_OBJECTS)
ifneq ($(strip $(USER_MODULE)),)
	@echo "\n===========>\t[$@]"
	$(LOCAL_CC) $(LOCAL_LDLIBS) -o $@ $^
endif
	
$(LOCAL_OBJECTS): $(LOCAL_OUT_DIR)/%.o : $(LOCAL_SRC_DIR)/%$(LOCAL_SUFFIX)
	$(LOCAL_CC) -c $(LOCAL_CFLAGS) -o $@ $<

-include $(LOCAL_DEPENDS)
$(LOCAL_DEPENDS) : $(LOCAL_OUT_DIR)/%.d : $(LOCAL_SRC_DIR)/%$(LOCAL_SUFFIX)
	@$(LOCAL_CC) -MM $(LOCAL_CFLAGS) $< > $@; \
	sed -i '1s#\($(basename $(notdir $*))\)\.o#$(LOCAL_OUT_DIR)/\1.o $(LOCAL_OUT_DIR)/\1.d#' $@

.PHONY : dump clean
dump:
	@echo LOCAL_C_INCLUDES = $(LOCAL_C_INCLUDES)
	@echo LOCAL_SRC_DIR = $(LOCAL_SRC_DIR)
	@echo LOCAL_OUT_DIR = $(LOCAL_OUT_DIR)
	@echo LOCAL_SUFFIX = $(LOCAL_SUFFIX)
	@echo LOCAL_EXCEPTS = $(LOCAL_EXCEPTS)
	@echo LOCAL_CFLAGS = $(LOCAL_CFLAGS)
	@echo LOCAL_LDLIBS = $(LOCAL_LDLIBS)
	@echo LOCAL_MODULE = $(LOCAL_MODULE)
	@echo LOCAL_SRC_FILES = $(LOCAL_SRC_FILES)
	@echo LOCAL_OBJECTS = $(LOCAL_OBJECTS)
	@echo LOCAL_DEPENDS = $(LOCAL_DEPENDS)

clean:
	-@rm -vf $(LOCAL_OBJECTS) $(LOCAL_DEPENDS) $(LOCAL_MODULE)
